﻿using bigscreen_ai_demo.DB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Windows;

namespace bigscreen_ai_demo
{
    public  class Config
    {
 
        public static string PythonPath { get; set; } = string.Empty;
        public static string PythonFilePath { get; set; } = string.Empty;

        public static string DBConnectionStr { set; get; } = string.Empty;
        /// <summary>
        /// 数据同步频率,毫秒
        /// </summary>
        public static int ReadJobTime { get; set; } = 2000;
        public static int SaveJobTime { get; set; } = 5000;

        public static IEnumerable<string> OutPutKeys { get; set; }

        public static ModelA ModelA_Default {  get; set; }

        public static string SXAIPostUrl { get; set; }
        public static string[] SXAIPostKeys {  get; set; }

        /// <summary>
        /// 模式
        /// </summary>
        public static Boolean SXAI_Allow { get; set; } = true;
        
        private static bool _sxaiShow = false;
        public static bool SXAI_Show
        {
            get => _sxaiShow;
            set
            {
                if (_sxaiShow != value)
                {
                    _sxaiShow = value;
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(nameof(SXAI_Show)));
                    });
                    
                }
            }
        }

        public static event PropertyChangedEventHandler StaticPropertyChanged;

        public static List<ModelA_OUTPUT_Map> modelA_OUTPUT_Maps { get; set; }

        //public static Dictionary<string, PropertyInfo> ModelAKeysProps = typeof(ModelA).GetProperties().Where(d => !new string[] { "Id", "State", "Created", "Updated" }.Contains(d.Name)).ToDictionary(d => d.Name);

        public static void Init() {
            var node = JsonNode.Parse(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "/config.json"));
            PythonPath = node["pythonPath"]?.GetValue<string>() ?? "";
            PythonFilePath = node["pyhonFilePath"]?.GetValue<string>() ?? "";
            if (PythonFilePath.Length!=0 && PythonFilePath.ElementAt(0) == '.') {
                PythonFilePath = AppDomain.CurrentDomain.BaseDirectory + PythonFilePath.Replace(".\\", "");
                File.Exists(PythonFilePath);
            }
            

            ReadJobTime = node["readJobTime"]?.GetValue<int>() ?? 5000;
            SaveJobTime = node["saveJobTime"]?.GetValue<int>() ?? 5000;
            OutPutKeys = node["outPutKeys"].AsArray().Select(d => d.GetValue<string>());
            SXAIPostKeys = node["sxaiPostKeys"].AsArray().Select(d => d.GetValue<string>()).ToArray();

            ModelA_Default = node["ModelA_default"].Deserialize<ModelA>();

            modelA_OUTPUT_Maps = node["ModelA_OUTPUT_Map"].Deserialize<List<ModelA_OUTPUT_Map>>();

            //SXAIPostUrl = node["sxaiPostUrl"]?.GetValue<string>() ?? "";
            SXAIPostUrl = "http://218.24.232.66:7073//predict";

            //DBConnectionStr = node["dbConnectionStr"]?.GetValue<string>() ?? "";
            DBConnectionStr = "Data Source = 218.24.232.66; Initial Catalog = qchg_ai_demo; Port=7777; User Id = admin; Password = mAiSrBXN63H6ifmM";

            //DBContext db = new DBContext();
            //db.CodeFirstSync();
            //if(db.Db.Queryable<ModelA>().First() == null)
            //{
            //    db.Db.Insertable(new ModelA()).ExecuteCommand();
            //}

            //if (db.Db.Queryable<ModelA_OUTPUT>().First() == null)
            //{
            //    db.Db.Insertable(new ModelA_OUTPUT()).ExecuteCommand();
            //}

        }
    }
}
